package com.until;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UntilClass {
    // 返回 连接 对象
    public static DruidPooledConnection getConnection() {
        ApplicationContext context = getContext();
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);
        DruidPooledConnection connection;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    // 返回 ICO context 对象
    public static ApplicationContext getContext() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        return context;
    }

    // 关闭连接 方法
    public static void connectionClose(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            // 关闭 执行对象、查询对象、连接对象
            if (null != statement) {
                statement.close();
            }
            if (null != resultSet) {
                resultSet.close();
            }
            if (null != connection) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
